package com.lianjing.sh.acceleo.controller.tool.service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Assert;
import org.junit.Test;

public class String_Service{
	
	private static String name0 = " ";
	private static String name1 = " efg ";
	private static String name2 = " {abc} = {wfg:Integer} xyz {uft} ";
	
	/**
	 *  {abc} = {wfg} xyz {uft} - Array[abc, wfg, uft]> 
	 */
	public List<String> getReplaceString(String name){
		List<String> list = new ArrayList<>();
		Matcher matcher = Pattern.compile("\\{[:\\w]+\\}").matcher(name);
		while(matcher.find()){
			String group = matcher.group();
			list.add(group.substring(1, group.length()-1).trim());
		}
		return list;
	}
	
	@Test
	public void testGetReplaceString() {
		String_Service service = new String_Service();
		Assert.assertEquals("[]", service.getReplaceString(name0).toString());
		Assert.assertEquals("[]", service.getReplaceString(name1).toString());
		Assert.assertEquals("[abc, wfg:Integer, uft]", service.getReplaceString(name2).toString());
	}
	
	/**
	 *  得到表达式字符串 
	 *  "/${packages.nameBody}/${packages.nameBody}/{id}/edit" - Array[packages.nameBody, packages.nameBody]> 
	 */
	public List<String> getExpressionString(String name){
		List<String> list = new ArrayList<>();
		if(null == name){
			return list;
		}
		Matcher matcher = Pattern.compile("\\$\\{[\\w\\.^~$]+\\}").matcher(name);
		while(matcher.find()){
			String group = matcher.group();
			list.add(group.substring(2, group.length()-1).trim());
		}
		return list;
	}
	
	@Test
	public void testGetExpressionString() {
		String_Service service = new String_Service();
		Assert.assertEquals("[]", service.getExpressionString(" ").toString());
		Assert.assertEquals("[]", service.getExpressionString(" /{id}/edit ").toString());
		Assert.assertEquals("[packages.nameBody, packages.nameBody]", service.getExpressionString(" /${packages.nameBody}/${packages.nameBody}/{id}/edit ").toString());
	}
	
	/**
	 * 替换
	 */
	public String replaces(String str, List<String> subStrs, List<String> replacements){
		if(replacements == null || subStrs == null || subStrs.isEmpty()|| replacements.isEmpty() || subStrs.size()!=replacements.size()){
			return str;
		}
		int i = 0;
		for(String item : replacements){
			str = str.replace(subStrs.get(i), item);
			i++;
		}
		return str;
	}
	
	/**
	 * 'abc' -> 	'abc'
	 * 'abc:efg' ->	'abc'
	 */
	public String getStrBefore(String str){
		int index = str.indexOf(":");
		if(index == -1){
			return str;
		}
		return str.substring(0,index);
	}
	
	@Test
	public void testGetStrBefore() {
		String_Service service = new String_Service();
		Assert.assertEquals("abc", service.getStrBefore("abc"));
		Assert.assertEquals("abc", service.getStrBefore("abc:efg"));
	}
	
	/**
	 * 'abc' -> ''
	 * 'abc:efg' ->'efg'
	 */
	public String getStrAfter(String str){
		int index = str.indexOf(":");
		if(index == -1){
			return "";
		}
		return str.substring(index+1);
	}
	
	@Test
	public void testGetStrAfter() {
		String_Service service = new String_Service();
		Assert.assertEquals("", service.getStrAfter("abc"));
		Assert.assertEquals("efg", service.getStrAfter("abc:efg"));
	}
	
}
